import { EventEmitter } from "./EventEmitter";
export class Sizes extends EventEmitter {
  constructor({ canvas }) {
    super();
    this.canvas = canvas;
    this.init();
    window.addEventListener("resize", () => {
      this.init();
      this.emit("resize");
    });
  }
  init() {
    // 获取CSS像素尺寸
    const width = this.canvas.parentNode.offsetWidth;
    const height = this.canvas.parentNode.offsetHeight;
    // 获取设备像素比
    const pixelRatio = window.devicePixelRatio || 1;
    this.width = width;
    this.height = height;
    this.pixelRatio = pixelRatio;
    // 设置canvas的实际像素尺寸
    this.canvas.width = width * pixelRatio;
    this.canvas.height = height * pixelRatio;
    // 设置canvas的style尺寸为CSS像素
    this.canvas.style.width = width + "px";
    this.canvas.style.height = height + "px";
  }
  destroy() {
    this.off("resize");
  }
}
